package nancal.mp.feign;


import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "nacos", url = "${spring.cloud.nacos.config.server-addr}")
public interface NacosClient {

    /**
     * 查询服务列表
     *
     * @param pageNo
     * @param pageSize
     * @param namespaceId 不传默认PUBLIC
     * @param groupName
     * @return
     */
    @GetMapping(value = "nacos/v1/ns/service/list", consumes = {"application/x-www-form-urlencoded"})
    String getServiceList(@RequestParam String pageNo, @RequestParam String pageSize, @RequestParam String namespaceId, @RequestParam String groupName);

    @GetMapping(value = "nacos/v1/ns/instance/list", consumes = {"application/x-www-form-urlencoded; charset=utf-8"})
    String getInstanceList(@RequestParam String namespaceId, @RequestParam String serviceName,
                           @RequestParam String groupName, @RequestParam String clusters, @RequestParam(defaultValue = "false") Boolean healthyOnly);


    /**
     * @param namespaceId
     * @param serviceName
     * @param groupName
     * @param ip
     * @param port
     * @param clusterName
     * @param weight
     * @param metadata
     * @param enabled
     * @param ephemeral
     * @return
     */
    @PutMapping(value = "nacos/v1/ns/instance", consumes = {"application/x-www-form-urlencoded; charset=utf-8"})
    String updateInstance(@RequestParam String namespaceId, @RequestParam String serviceName, @RequestParam String groupName,
                          @RequestParam String ip, @RequestParam Integer port, @RequestParam String clusterName, @RequestParam Double weight,
                          @RequestParam Object metadata, @RequestParam Boolean enabled, @RequestParam Boolean ephemeral);


    @GetMapping(value = "/nacos/v1/ns/service", consumes = {"application/x-www-form-urlencoded"})
    String getServiceDetail(@RequestParam String serviceName, @RequestParam String groupName, @RequestParam String namespaceId);


    /**
     * 查询实例列表
     *
     * @param pageNo
     * @param pageSize
     * @param namespaceId 不传默认PUBLIC
     * @param groupName
     * @return
     */
    @GetMapping(value = "/nacos/v1/ns/instance/list", consumes = {"application/x-www-form-urlencoded"})
    String getInstanceList(@RequestParam String pageNo, @RequestParam String pageSize, @RequestParam String namespaceId, @RequestParam String groupName);


    @PostMapping(value = "nacos/v1/auth/users/login", consumes = {"application/x-www-form-urlencoded"})
    String doLogin(@RequestParam String username, @RequestParam String password);



    @PostMapping(value = "nacos/v1/auth/users", consumes = {"application/x-www-form-urlencoded; charset=utf-8"})
    String createUser(@RequestParam String username, @RequestParam String password, @RequestParam String accessToken);

    @PostMapping(value = "nacos/v1/auth/roles", consumes = {"application/x-www-form-urlencoded; charset=utf-8"})
    String createRole(@RequestParam String role, @RequestParam String username, @RequestParam String accessToken);

    @PostMapping(value = "nacos/v1/auth/permissions", consumes = {"application/x-www-form-urlencoded; charset=utf-8"})
    String createSource(@RequestParam String role, @RequestParam String resource, @RequestParam String action, @RequestParam String accessToken);

    @PostMapping(value = "nacos/v1/console/namespaces", consumes = {"application/x-www-form-urlencoded; charset=utf-8"})
    String createNamespace(@RequestParam String customNamespaceId, @RequestParam String namespaceName, @RequestParam String namespaceDesc, @RequestParam String namespaceId, @RequestParam String accessToken);


    @GetMapping(value = "nacos/v1/ns/catalog/instances", consumes = {"application/x-www-form-urlencoded; charset=utf-8"})
    String getInstances(@RequestParam String serviceName, @RequestParam String clusterName, @RequestParam String groupName,
                        @RequestParam Integer pageSize, @RequestParam Integer pageNo, @RequestParam String namespaceId);

    @GetMapping(value = "nacos/v1/cs/history", consumes = {"application/x-www-form-urlencoded; charset=utf-8"})
    String getConfigHistoryList(@RequestParam Integer pageNo, @RequestParam Integer pageSize, @RequestParam String tenant,
                                @RequestParam String search, @RequestParam String dataId, @RequestParam String group);

    @GetMapping(value = "nacos/v1/cs/history", consumes = {"application/x-www-form-urlencoded; charset=utf-8"})
    String getConfigHistoryDetail(@RequestParam Integer nid, @RequestParam String tenant, @RequestParam String dataId, @RequestParam String group);

    @GetMapping(value = "nacos/v1/cs/history/previous", consumes = {"application/x-www-form-urlencoded; charset=utf-8"})
    String getConfigHistoryPrevious(@RequestParam Integer id);

    /**
     * 获取 Nacos 中全部服务
     *
     * @param namespaceId      命名空间id
     * @param withInstances    是否显示服务实例
     * @param pageNo           当前页
     * @param pageSize         页大小
     * @param serviceNameParam 服务名称
     * @param groupNameParam   分组名称
     * @param hasIpCount       是否过滤空服务
     * @return Json
     */
    @GetMapping(value = "nacos/v1/ns/catalog/services", consumes = {"application/x-www-form-urlencoded; charset=utf-8"})
    String getAllServicesAndInstance(@RequestParam String namespaceId, @RequestParam Boolean withInstances,
                                     @RequestParam Integer pageNo, @RequestParam Integer pageSize,
                                     @RequestParam String serviceNameParam, @RequestParam String groupNameParam,
                                     @RequestParam Boolean hasIpCount);

    /**
     * 获取 Nacos 中某个服务
     *
     * @param namespaceId 命名空间id
     * @param serviceName 服务名称
     * @param groupName   分组名称
     * @return Json
     */
    @GetMapping(value = "nacos/v1/ns/catalog/service", consumes = {"application/x-www-form-urlencoded; charset=utf-8"})
    String getService(@RequestParam String namespaceId, @RequestParam String serviceName, @RequestParam String groupName);
}
